/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.hboxs.asl.qcloudLVB.entity.Live;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Entity - 机器人
 */
@Entity
@Table(name = "xx_robot")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_robot_sequence")
public class Robot extends OrderEntity {

    private static final long serialVersionUID = -6109509834123943215L;

    /**
     * 头像
     */
    private String avator;

    /**
     * 昵称
     */
    private String name;

    /**
     * 观看过的直播
     */
    private Set<Live> watchedLives = new HashSet<>();

    public String getAvator() {
        return avator;
    }

    public void setAvator(String avator) {
        this.avator = avator;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    /**
     * 获取观看过的直播
     *
     * @return 观看过的直播
     */
    @ManyToMany(mappedBy = "watchedRobots", fetch = FetchType.LAZY)
    public Set<Live> getWatchedLives() {
        return watchedLives;
    }

    /**
     * 设置观看过的直播
     *
     * @param watchedLives 观看过的直播
     */
    @Deprecated
    public void setWatchedLives(Set<Live> watchedLives) {
        this.watchedLives = watchedLives;
    }

    /**
     * 删除前处理
     */
    @PreRemove
    public void preRemove() {

        Set<Live> watchedLives =  getWatchedLives();
        if (watchedLives != null) {
            for (Live live : watchedLives) {
                live.getWatchedRobots().remove(this);
            }
        }
    }
}